<?php

/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     pagizer
 * @subpackage  website
 */

class m_website_documents_Website extends m_website_documents_WebsiteBase
{
	/**
	 * @return m_website_documents_Website
	 */
	public static function createNew()
	{
		return new self();
	}
	
	/**
	 * @return m_website_documents_Website
	 */
	public static function getInstanceByUniqueId($id, $lang = null)
	{
		if(is_null($lang)) $lang = f_core_Context::getInstance()->getLang();
		$cache = f_core_Cache::getInstance();
		
		$key = $cache->getDocumentCacheKey($id, $lang);
		
		if($cache->hasCache($key))
		{
			return $cache->getCacheContent($key);
		}
		
		$object = new self($id, $lang);
		
		$cache->putInCache($key, $object);
		
		return $object;
	}

    public function getConfigModule()
    {
        return f_core_Request::getInstance()->getParameter('configModule', 'website');
    }

    public function isOverLoaded()
    {       
        return ($this->getConfigModule() != 'website');
    }
	
	public function canSlide()
	{
		return true;
	}
	
	public function getIcon()
	{
        return 'website.png';
	}

	public function getNewDocumentLabel()
	{
		return '';
	}		

    public function getContextEditUrl($lang = null, $tabName = null)
    {
        if(is_null($lang))
		{
			$lang = f_core_Request::getInstance()->getParameter("docLang", $this->getLang());
		}

		if(is_null($tabName))
		{
			$tabName = "properties";
		}


    }

	public function getEditUrl($lang = null, $tabName = null)
	{
		if(is_null($lang))
		{
			$lang = f_core_Request::getInstance()->getParameter("docLang", $this->getLang());
		}
		
		if(is_null($tabName))
		{
			$tabName = "properties";
		}

        if($this->isOverLoaded())
        {
            $moduleTreeParser = f_file_Resolver::getModuleFilePath($this->getConfigModule(), 'config'.DS.'treeParser.xml');
            if(!is_null($moduleTreeParser))
            {
                $rootNode = f_xml_Xpath::getInstance($moduleTreeParser)->getElementFromXpath('rootNode');

                if(!is_null($rootNode))
                {
                    $route = $rootNode->getAttributeValue('route', 'genericWebsiteFolderList');
                    return f_core_Routing::getUrl($route, array("configModule" => $this->getConfigModule(), "docId" => $this->getUniqueId(), "docLang" => strtolower($this->getVO())));
                }

            }

            return f_core_Routing::getUrl("genericWebsiteFolderList", array("configModule" => $this->getConfigModule(), "docId" => $this->getUniqueId(), "docLang" => strtolower($this->getVO())));

        }

		return f_core_Routing::getUrl("websiteEdit", array("tabName" => $tabName, "docId" => $this->getUniqueId(), "docLang" => strtolower($lang)));
	}
	
	public function getDeleteUrl($lang = null)
	{
		if(is_null($lang))
		{
			$lang = f_core_Request::getInstance()->getParameter("docLang");
		}

        if($this->isOverLoaded()) return '';

		return f_core_Routing::getUrl("deleteWebsite", array("docId" => $this->getUniqueId(), "docLang" => strtolower($lang)));
	}

    public function getLabel()
    {
        if($this->isOverLoaded())
        {
            if($this->isATranslation())
            {
                return $this->getVoDocument()->getLabel();
            }
        }

        return parent::getLabel();
    }

    public function isTranslated()
    {
        if($this->isOverLoaded())
        {
            return true;
        }

        return parent::isTranslated();
    }
	
	public function getRealUrl()
	{
		if($this->isATranslation())
		{
			$website = f_document_Provider::getInstance()->getByModelAndUniqueId('website/Website', $this->getUniqueId(), $this->getVo());
			return $website->getUrl()."/".strtolower($this->getLang());
		}
		
		return $this->getUrl();
	}

    public function getHomePage()
    {
        $homePage = f_document_Loader::getInstance('modules_website/page')
                    ->whereIsEqual('websiteId', $this->getUniqueId())
                    ->whereIsEqual('url', '/')
                    ->retrieveFirstDocument($this->getLang());

        return $homePage;
    }
	
	public function getTranslatedWebsites()
	{
		$result = array();
		
		$langs = $this->getTranslationLangs();
		
		if(is_array($langs) && count($langs) > 0)
		{
			foreach($langs as $lang)
			{
				$result[] = f_document_Provider::getInstance()->getByModelAndUniqueId('website/Website', $this->getUniqueId(), $lang);
			}			
		}
		
		return $result;
	}

    /**
	 * Magic PHP5 method to simulate add and get
	 *
	 * @param string $method
	 * @param mixed $attr
	 * @return mixed
	 */
	public function __call($method, $attr)
	{
		$prefix = substr($method, 0, 3);
		$suffix = strtolower(substr($method, 3));

		if($prefix == "add")
		{
			f_relation_Manager::getInstance()->setRelation($this, $attr[0], $suffix, $attr[1], $this->getLang());
			return $this;
		}
		else if($prefix == "get")
		{
			if(substr($suffix, -2, 2) == "nb")
			{
				return f_relation_Manager::getInstance()->getChildsNb($this, substr($suffix, 0, -2), $this->getLang());
			}

            $nbResult = isset($attr[0]) ? $attr[0] : null;
            $offset = isset($attr[1]) ? $attr[1] : null;
            $order = isset($attr[2]) ? $attr[2] : null;

			return f_relation_Manager::getInstance()->getChilds($this, $suffix, $this->getLang(), $nbResult, $offset, $order);
		}
	}

    public function getBoSearchIcon()
    {
        return m_medias_services_MediaService::getInstance()->getIconPath($this->getIcon());
    }
}